package com.nowcoder.community.service;

import com.nowcoder.community.dao.CommentMapper;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @ClassName CommentService
 * @Description
 * @Author hhwu
 * @Date 2022/5/23 16:26
 * Version 1.0
 **/
@Service
public class CommentService implements CommunityConstant{

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private DiscussPostService discussPostService;

    public List<Comment> findCommentsByEntity(int entityType, int entityId, int offset, int limit) {
        /**
         * @Author hhwu
         * @Description //TODO 查询指定类型的实体的和指定类型的实体id的评论的每页内容
         * @Date 16:33 2022/5/23
         * @Param [entityType, entityId, offset, limit]
         * @return java.util.List<com.nowcoder.community.entity.Comment>
         **/
        return commentMapper.selectCommentsByEntity(entityType, entityId, offset, limit);
    }

    public int findCommentCount(int entityType, int entityId) {
        /**
         * @Author hhwu
         * @Description //TODO 查询指定类型的实体的和指定类型的实体id的评论数量
         * @Date 16:34 2022/5/23
         * @Param [entityType, entityId]
         * @return int
         **/
        return commentMapper.selectCountByEntity(entityType, entityId);
    }

    // 声明式事务
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)// 四种隔离级别，注意用中间2种。传播方式主要用REQUIRED或REQUIRES_NEW
    public int addComment(Comment comment) {
        /**
         * @Author hhwu
         * @Description //TODO 涉及事务管理操作：1。 添加对帖子的评论内容； 2. 修改该帖子的评论数量。使用spring管理事务有2种方式：1.声明式事务（将整个方法作为一个事务）；2.编程式事务（将方法的局部作为一个事务）
         * @Date 22:18 2022/5/23
         * @Param [comment]
         * @return int
         **/
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空！");
        }

        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));// 过滤用户类似sql注入的文本
        comment.setContent(sensitiveFilter.filter(comment.getContent()));// 过滤敏感词
        int rows = commentMapper.insertComment(comment);

        // 更新帖子的评论数量
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
            discussPostService.updateCommentCount(comment.getEntityId(), count);
        }
        return rows;
    }

    public Comment findCommentById(int id) {
        /**
         * @Author hhwu
         * @Description //TODO 通过id查询评论
         * @Date 11:45 2022/5/27
         * @Param [id]
         * @return com.nowcoder.community.entity.Comment
         **/
        return commentMapper.selectCommentById(id);
    }
}
